<?php

if(!defined("IN_PHPBB")) die;

$catid=request_var('catid',0,true);
$action = request_var('action','',true);

if($action=="create")
{
	$catname=request_var("catname","",true);
	$tag=request_var("tag","",true);
	$folder=request_var("folder","",true);
	Category::Create($catname,$tag,$folder,$catid);
	echo '			<div class="notification success png_bg">
				<a href="#" class="close"><img src="resources/images/icons/cross_grey_small.png" title="Close this notification" alt="close" /></a>
				<div>' .
					$catname . " was created successfully!" . '
				</div>
			</div>';
}

if($action=="delete")
{
	if(isset($_REQUEST['select_ids']))
	{
		$ids=$_REQUEST['select_ids'];
        foreach($ids as $id => $val)
        {
            Category::Delete($val);            
        }
	}
	$action="";
}
echo '<h3>';
$cat = new Category($catid);

$updatsuccess=false;
if(isset($_REQUEST['update']))
{
	$catname=request_var("catname","",true);
	$tag=request_var("tag","",true);
	$folder=request_var("folder","",true);
    $updatsuccess=$cat->Update($catname,$folder,$tag);
}

//categories path
for($j=0;$j<count($cat->path);$j++)
	echo '<a href="index.php?p=cat&catid=' . $cat->path[$j]['id'] . '">'. $cat->path[$j]['catname'] . "</a> <strong>&#8249;</strong> ";
echo '<a href="index.php?p=cat&catid=' . $cat->data['id'] . '">'. $cat->data['catname'] . '</a></h3><div class="clear"></div>';
//xong

if(isset($_REQUEST['update']))
{
    if($updatsuccess)
	   echo '			<div class="notification success png_bg">
				<a href="#" class="close"><img src="resources/images/icons/cross_grey_small.png" title="Close this notification" alt="close" /></a>
				<div>' .
					$catname . " was saved successfully!" . '
				</div>
			</div>';
}
?>
<script type="text/javascript">
//<![CDATA[
	$(document).ready(function(){
			currentTab = $('.content-box ul.content-box-tabs li a.current').attr('href');
			$(currentTab).show();
	});
//]]>
</script>
<div class="content-box"><!-- Start Content Box -->
				<div class="content-box-header">
					
					<h3><?php echo $cat->data['catname']; ?></h3>
					<ul class="content-box-tabs">
						<li><a href="#tab1" <?php if($action=="") echo ' class="default-tab"'; ?>>Sub categories</a></li> <!-- href must be unique and match the id of target div -->
						<li><a href="#tab2" <?php if($action=="create") echo ' class="default-tab"'; ?>>Create sub category</a></li>
						<?php
						if(isset($cat->data))
						{
						?>
						<li><a href="#tab3" <?php if($action=="edit") echo ' class="default-tab"'; ?>>Edit Info</a></li>
						<?php
						}
						?>
					</ul>					
					<div class="clear"></div>
					
				</div> <!-- End .content-box-header -->
				
				<div class="content-box-content">
					
					<div class="tab-content" id="tab1"> <!-- This is the target div. id must match the href of this div's tab -->
						<form action="index.php?p=cat&catid=<?php echo $catid; ?>&action=delete" method="post">
						<table>
							
							<thead>
								<tr>
								   <th><input class="check-all" type="checkbox" /></th>
								   <th>Category name</th>
								   <th>Tag</th>
								   <th>Folder</th>
								   <th>More</th>
								   <th>Actions</th>
								</tr>
								
							</thead>
						 
							<tfoot>
								<tr>
									<td colspan="6">
										<div class="bulk-actions align-left">
											<!--a class="button" href="#">Delete</a-->
											<input type="submit" class="button" value="Delete" />
										</div>
										<div class="clear"></div>
									</td>
								</tr>
							</tfoot>
						 
							<tbody>
							<?php
								foreach($cat->sub as $name => $row)
								{
									
							?>
								<tr>
									<td><input type="checkbox" name="select_ids[]" value="<?php echo $row['id']; ?>"/></td>
									<td><a href="index.php?p=cat&catid=<?php echo $row['id']; ?>"><?php echo $row['catname']; ?></a></td>
									<td><a href="#" title="title"><?php echo $row['tag']; ?></a></td>
									<td><?php echo $row['folder']; ?></td>
									<td><a href="index.php?p=items&catid=<?php echo $row['id']; ?>&action=view">View items</a></td>
									<td>
										<!-- Icons -->
										 <a href="index.php?p=cat&catid=<?php echo $row['id']; ?>&action=edit" title="Edit"><img src="resources/images/icons/pencil.png" alt="Edit" /></a>
										 <a href="index.php?p=cat&catid=<?php echo $row['id']; ?>&action=del" title="Delete"><img src="resources/images/icons/cross.png" alt="Delete" /></a> 
									</td>
								</tr>
							<?php
								}
							?>
							</tbody>
							
						</table>
						</form>
					</div> <!-- End #tab1 -->
					<div class="tab-content" id="tab2">
						<form action="index.php?action=create&catid=<?php echo $catid; ?>" method="post">
							
							<fieldset> <!-- Set class to "column-left" or "column-right" on fieldsets to divide the form into columns -->
								<p>
									<label>Category Name</label>
									<input class="text-input small-input" type="text" name="catname" /> <!-- Classes for input-notification: success, error, information, attention -->
								</p>
								
								<p>
									<label>Folder</label>
									<input class="text-input small-input" type="text" name="folder" /> <!-- Classes for input-notification: success, error, information, attention -->
								</p>
								<p>
									<label>Tag</label>
									<input class="text-input small-input" type="text" name="tag" />
								</p>
								<p>
									<input class="button" type="submit" value="Create" />
								</p>
								
							</fieldset>
							
							<div class="clear"></div><!-- End .clear -->
							
						</form>
					</div>
					<div class="tab-content" id="tab3">
						<form action="index.php?update=1&catid=<?php echo $catid; ?>" method="post">
							
							<fieldset> <!-- Set class to "column-left" or "column-right" on fieldsets to divide the form into columns -->
								<p>
									<label>Category Name</label>
									<input class="text-input small-input" type="text" name="catname" value="<?php echo $cat->data['catname']; ?>" /> <!-- Classes for input-notification: success, error, information, attention -->
								</p>
								
								<p>
									<label>Folder</label>
									<input class="text-input small-input" type="text" name="folder" value="<?php echo $cat->data['folder']; ?>" /> <!-- Classes for input-notification: success, error, information, attention -->
								</p>
								<p>
									<label>Tag</label>
									<input class="text-input small-input" type="text" name="tag" value="<?php echo $cat->data['tag']; ?>" />
								</p>
								<p>
									<input class="button" type="submit" value="Save" />
								</p>
								
							</fieldset>
							
							<div class="clear"></div><!-- End .clear -->
							
						</form>
					</div>
				</div> <!-- End .content-box-content -->
				
			</div> <!-- End .content-box -->